package rpcserver

import (
	rpcv1 "gluenet/internal/agent/api/rpc/v1"
	"gluenet/internal/agent/depend"
	"gluenet/internal/agent/driver/rpcserver"
	"gluenet/internal/agent/service/promsvc"
	"gluenet/utils/rpc/v2/define"
)

const (
	prom = rpcv1.RPC_SERVICE_PROM
)

func init() {
	depend.Provide(NewPromRouter)
	registerRouter(prom, func(c rpcserver.IRoutes) {
		depend.Invoke(func(d *promRouter) {
			d.initRouter(c)
		})
	})
}

type promRouter struct {
	prom *promsvc.Service
}

func NewPromRouter(svc *promsvc.Service) *promRouter {
	return &promRouter{svc}
}

func (r *promRouter) initRouter(c rpcserver.IRoutes) {
	c.Group(`prom`).
		Func(`query`, r.promQuery).
		Func(`query_range`, r.promQueryRange)
}

func (r *promRouter) promQuery(c *rpcserver.Context) {
	promQueryReq := new(define.PromQuery)
	if err := c.BindJSON(&struct {
		Spec interface{} `json:"spec,omitempty"`
	}{promQueryReq}); err != nil {
		return
	}
	resp := r.prom.PromQuery(*promQueryReq)
	c.JSON(resp)
}

func (r *promRouter) promQueryRange(c *rpcserver.Context) {
	promQueryRangeReq := new(define.PromQueryRange)
	if err := c.BindJSON(&struct {
		Spec interface{} `json:"spec,omitempty"`
	}{promQueryRangeReq}); err != nil {
		return
	}
	resp := r.prom.PromQueryRange(*promQueryRangeReq)
	c.JSON(resp)
}
